/**
 * 
 */
package com.kebo2.client;

import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;

/**
 * @author Tim
 *This class creates the UI after a user is logged in
 */
public class UIAfterLogin extends UserInterface {
	
	private Button addButton;
	private Button editButton;
	private Button deleteButton;
	private Button editThemesButton;
	private Button searchButton;
	private Button printIdButton;
	private FlexTable modeSelector;
	private RadioButton rdbtnBooks;
	private RadioButton rdbtnUsers;
	private HTML lblSelecteerEenModus;
	private Button trashButton;
	private Button bookInButton;
	private Button bookOutButton;
	
	public UIAfterLogin() {
		RootLayoutPanel rootPanel = RootLayoutPanel.get();
		rootPanel.setSize("100%", "100%");
		
		mainPanel = new DockLayoutPanel(Unit.EM);
		rootPanel.add(mainPanel);
		mainPanel.setSize("100%", "100%");
		
		dock = new HorizontalPanel();
		dock.setStyleName("dock");
		mainPanel.addNorth(dock, 6.3);
		dock.setSize("100%", "100px");
		
		addButton = new Button("");
		addButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Add(rdbtnBooks.getValue());
			}
		});
		addButton.setTitle("Voeg media toe ");
		addButton.setStyleName("gwt-Button-add");
		dock.add(addButton);
		addButton.setSize("48px", "48px");
		
		editButton = new Button("");
		editButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Edit(rdbtnBooks.getValue());
			}
		});
		editButton.setTitle("Bewerk media");
		editButton.setStyleName("gwt-Button-edit");
		editButton.setText("");
		dock.add(editButton);
		editButton.setSize("48px", "48px");
		
		deleteButton = new Button("");
		deleteButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Delete(rdbtnBooks.getValue());
			}
		});
		deleteButton.setTitle("Verwijder media");
		deleteButton.setStyleName("gwt-Button-delete");
		dock.add(deleteButton);
		deleteButton.setSize("48px", "48px");
		
		searchButton = new Button("");
		searchButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Search(rdbtnBooks.getValue(), 0);
			}
		});
		searchButton.setTitle("Zoek media");
		searchButton.setStyleName("gwt-Button-search");
		dock.add(searchButton);
		searchButton.setSize("48px", "48px");
		
		editThemesButton = new Button("");
		editThemesButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				boolean currentMode = rdbtnBooks.getValue();
				if(currentMode == true) 
					new EditThemes();
				else
					new EditGroups();
			}
		});
		editThemesButton.setTitle("Bewerk thema's");
		editThemesButton.setStyleName("gwt-Button-editThemes");
		dock.add(editThemesButton);
		editThemesButton.setSize("48px", "48px");
		
		printIdButton = new Button("New button");
		printIdButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new PrintIds();
			}
		});
		printIdButton.setTitle("Print etiketten");
		printIdButton.setStyleName("gwt-Button-printId");
		printIdButton.setText("");
		dock.add(printIdButton);
		printIdButton.setSize("48px", "48px");
		
		trashButton = new Button("New button");
		trashButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Trash();
			}
		});
		trashButton.setTitle("Bekijken verwijderde media");
		trashButton.setStyleName("gwt-Button-trash");
		trashButton.setText("");
		dock.add(trashButton);
		trashButton.setSize("48px", "48px");
		
		bookInButton = new Button("");
		bookInButton.setTitle("Breng media binnen");
		bookInButton.setVisible(false);
		bookInButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new BookIn();
			}
		});
		bookInButton.setStyleName("gwt-Button-bookIn");
		dock.add(bookInButton);
		bookInButton.setSize("48px", "48px");
		
		bookOutButton = new Button("");
		bookOutButton.setTitle("Leen media uit");
		bookOutButton.setVisible(false);
		bookOutButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new BookOut();
			}
		});
		bookOutButton.setStyleName("gwt-Button-bookOut");
		dock.add(bookOutButton);
		bookOutButton.setSize("48px", "48px");
		
		modeSelector = new FlexTable();
		dock.add(modeSelector);
		
		rdbtnBooks = new RadioButton("modeSelector", "Boeken");
		rdbtnBooks.setValue(true);
		rdbtnBooks.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				boolean currentMode = rdbtnBooks.getValue();
				if(currentMode == true)
					booksMode();
				else
					usersMode();
			}
		});
		rdbtnBooks.setEnabled(true);
		modeSelector.setWidget(0, 0, rdbtnBooks);
		
		rdbtnUsers = new RadioButton("modeSelector", "Gebruikers");
		rdbtnUsers.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				boolean currentMode = rdbtnBooks.getValue();
				if(currentMode == true)
					booksMode();
				else
					usersMode();
			}
		});
		modeSelector.setWidget(0, 1, rdbtnUsers);
		
		lblSelecteerEenModus = new HTML("<i>(Selecteer een modus)</i>");
		modeSelector.setWidget(0, 2, lblSelecteerEenModus);
		
		systembar = new DockLayoutPanel(Unit.EM);
		systembar.setStyleName("systembar");
		mainPanel.addSouth(systembar, 2.5);
		systembar.setSize("100%", "30px");
		
		userButton = new Button("");
		userButton.setStyleName("gwt-Button-user");
		systembar.addEast(userButton, 3.4);
		userButton.setSize("32px", "32px");
		
		menu = new FlowPanel();
		menu.setStyleName("menu");
		mainPanel.addNorth(menu, 1.7);
		menu.setSize("100%", "23px");
		
		iconFlexTable = new FlexTable();
		menu.add(iconFlexTable);
		
		closeImg = new Image("img/close.png");
		closeImg.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UserInterface.closeWindow();
				UserInterface.clearIcons();
			}
		});
		closeImg.setTitle("Sluit");
		closeImg.setStyleName("iconImg");
		iconFlexTable.setWidget(0, 0, closeImg);
		
		closeTxt = new HTML("Sluit");
		closeTxt.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UserInterface.closeWindow();
				UserInterface.clearIcons();
			}
		});
		closeTxt.setStyleName("iconTxt");
		iconFlexTable.setWidget(0, 1, closeTxt);
		
		exportImg = new Image("img/export.png");
		exportImg.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Export();
			}
		});
		exportImg.setTitle("Exporteer");
		exportImg.setStyleName("iconImg");
		iconFlexTable.setWidget(0, 2, exportImg);
		
		exportTxt = new HTML("Exporteer");
		exportTxt.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Export();
			}
		});
		exportTxt.setStyleName("iconTxt");
		iconFlexTable.setWidget(0, 3, exportTxt);
		
		window = new DockLayoutPanel(Unit.EM);
		window.setVisible(false);
		window.setStyleName("window");
		mainPanel.add(window);
		
		new UserInterface.notification("Welkom.");
	}

	protected void usersMode() {
		printIdButton.setVisible(false);
		trashButton.setVisible(false);
		bookInButton.setVisible(true);
		bookOutButton.setVisible(true);
		
		addButton.setTitle("Voeg een gebruiker toe");
		editButton.setTitle("Bewerk een gebruiker");
		deleteButton.setTitle("Verwijder een gebruiker");
		searchButton.setTitle("Zoek een gebruiker");
		editThemesButton.setTitle("Bewerk groepen");	
	}

	protected void booksMode() {
		printIdButton.setVisible(true);
		trashButton.setVisible(true);
		bookInButton.setVisible(false);
		bookOutButton.setVisible(false);
		
		addButton.setTitle("Voeg media toe");
		editButton.setTitle("Bewerk media");
		deleteButton.setTitle("Verwijder media");
		searchButton.setTitle("Zoek media");
		editThemesButton.setTitle("Bewerk thema's");	
	}
}
